<template>
  <div>
    <el-dialog :model-value="visible" :close-on-click-modal="false" title="调拨单明细" width="1100" @close="cls">
      <el-button type="primary" size="small" style="float: right; margin-top: -30px; margin-right: 30px;"
        @click="removeById">删除</el-button>
      <!-- 数据列表 -->
      <el-table :data="tableData" style="width: 100%; height: 400px;" border stripe @selection-change="removeId">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="billDate" label="单据日期" width="100" header-align="center" align="center" />
        <el-table-column prop="code" label="单据编号" width="100" header-align="center" align="center" />
        <el-table-column prop="goodsName" label="商品名称" width="100" header-align="center" align="center" />
        <el-table-column prop="num" label="数量" width="100" header-align="center" align="center" />
        <el-table-column prop="unit" label="单位" width="100" header-align="center" align="center" />
        <el-table-column prop="outName" label="调出仓库" width="100" header-align="center" align="center" />
        <el-table-column prop="inName" label="调入仓库" width="100" header-align="center" align="center" />
        <el-table-column prop="user" label="制单人" width="100" header-align="center" align="center" />
        <el-table-column prop="audit" label="审核人" width="100" header-align="center" align="center" />
        <el-table-column label="操作" header-align="center" align="center">
          <template v-slot="{ row }">
            <span class="btn" @click="removeById(row.id)">删除</span>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script setup>


import { ref,onMounted } from "vue";
import { DeleteAllocatesDetails, allocateList } from "@/api/warehouse/list.jsx";
import { ElMessage, ElMessageBox } from "element-plus";
import { getUser } from "@/utils/session";

const tableData = ref([])
//声明事件
const emits = defineEmits(["close", "refresh"])
const props = defineProps({
  "visible": {
    type: Boolean,
    default: false
  }
})

//暴露
defineExpose({ fundXx })

//关闭窗口
function cls() {
  //抛出自定义事件
  emits("close");
}

async function fundXx(id) {
  let data = {
    id: id,
    eid:getUser().eid
  }
  let res = await allocateList(data)
  if (res.code == 200) {
    
    res.data.list.forEach(item => {
      if (item.id == id) {
        tableData.value = item.allocateDetailList
      }
    });
  }
}


//批量删除
let rmData = []
//获取删除的id
function removeId(ids) {
  let data = [];
  for (let i = 0; i < ids.length; i++) {
    data.push(ids[i].id)
  }
  rmData = data
}

function removeById(id) {
  if (isNumber(id)){
    rmData.push(id)
  }
  ElMessageBox.confirm(
    '确认删除该调拨单明细信息吗，删除后该调拨单明细的所有数据将无法恢复！',
    '调拨单删除确认',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    } 
    
  )
    .then(async () => {
      if (rmData.length == 0) {
        ElMessage({
          type: 'warning',
          message: '请选择要删除的调拨单明细',
        })
        return
      }
      let res = await DeleteAllocatesDetails(rmData)

      if (res.code == 200) {
        ElMessage({
          type: 'success',
          message: '删除成功',
        })

      }
      rmData = []
      cls()
    })
    .catch(() => {

    })
}



//判断当前值是否是数字类型
function isNumber(value) {
  return !isNaN(parseFloat(value)) && isFinite(value);
}

onMounted(() => {
  getUser();
})

</script>

<style scoped lang="scss">
.btn {
  color: #3399FF;
  margin-left: 10px;
  cursor: pointer;
}

.toolbar {
  float: right;
}
</style>
